package com.ruoyi.common.sms.service;

import com.ruoyi.common.core.constant.SecurityConstants;
import com.ruoyi.common.redis.service.RedisService;
import com.ruoyi.common.sms.domain.Code;
import com.ruoyi.common.sms.utils.SMSUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

/**
 * @author fxy
 * @date 2024/9/12
 * @Description
 */
@Service("SMSService")
public class SMSService {
    @Autowired
    private RedisService redisService;
    public static void sendByShop(String phone) {
        SMSUtil.sendVerificationCodeByShop(phone);
    }
    public static void sendByUser(String phone) {
        SMSUtil.sendVerificationCodeByUser(phone);
    }
    public  void sendVerificationCodeByUser(String phone) {
        System.out.println("传入的phone = " + phone);
        String code = SMSUtil.generateFourDigitCode(); // 生成验证码
        System.out.println("生成的验证码 = " + code);
        //用户验证码存入Redis，设置3分钟过期时间
        redisService.setCacheObject(SecurityConstants.LOGIN_CODE_KEY +phone, code);
        try {
            Thread.sleep(2000); // 延迟 1 秒
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
        System.out.println("redisService.getCacheObject(phone) = " + redisService.getCacheObject(SecurityConstants.LOGIN_CODE_KEY +phone));
        //redisService.setCacheObject(phone, code,5L,TimeUnit.MINUTES);
        //redisService.setValueWithExpiration(phone, code, 3, TimeUnit.MINUTES);
        Code param = new Code(code);
        //开启短信服务验证
        /*
        * 先注释掉，已经测试通过，不需要再调用，只需要输入自动生成的验证码即可进行登录
        * */
        //SMSUtil.sendSms(phone, "阿里云短信测试", "SMS_154950909", JSON.toJSONString(param)); // 发送验证码
    }
}
